import traceback

from fastapi import FastAPI

from ..register import register_mount, register_cors, register_exception, register_middleware, register_router
from ..core.config import settings
from ..common import logger


def create_app() -> FastAPI:
    """
    生成FastAPI对象
    :return: 生成FastAPI对象
    """

    app = FastAPI(
        debug=settings.DEBUG,
        title=settings.TITLE,
        description=settings.DESCRIPTION,
        docs_url=settings.DOCS_URL,
        openapi_url=settings.OPENAPI_URL,
        redoc_url=settings.REDOC_URL
    )

    # 其余的一些全局配置可以写在这里 多了可以考虑拆分到其他文件夹
    # 注册静态文件
    register_mount(app)

    # 跨域设置
    register_cors(app)

    # 注册捕获全局异常
    register_exception(app)

    # 请求拦截
    register_middleware(app)

    # 注册路由
    register_router(app)

    logger.info("日志初始化成功！！！")  # 初始化日志

    return app
